package com.chare.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chare.mapper.EmployeeMapper;
import com.chare.pojo.Employee;
import com.chare.pojo.RespBean;
import com.chare.pojo.RespPageBean;
import com.chare.pojo.Salary;
import com.chare.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author shi
 * @since 2022-06-17
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {
@Autowired
private EmployeeMapper employeeMapper;
/*@Autowired
private RabbitTemplate rabbitTemplate;*/
    @Override
    public RespPageBean getEmployeeByPage(Integer page, Integer pageSize, Employee employee, LocalDateTime[] beginDateScope) {

        //开启分页
        Page pageInfo=new Page(page,pageSize);
        IPage<Employee> employeePage= employeeMapper.getEmployeeByPage(pageInfo,beginDateScope,employee);
        RespPageBean respPageBean=new RespPageBean(employeePage.getTotal(),employeePage.getRecords());

        return respPageBean;
    }

    @Override
    public RespBean maxWorkId() {
        List<Map<String,Object>> maps = employeeMapper.selectMaps(new QueryWrapper<Employee>().select("MAX(wokeID)"));
        return RespBean.success("null",String.format("%08d",Integer.parseInt(maps.get(0).get("MAX(wokeID)").toString()+1)));
    }

    @Override
    public RespBean insertEm(Employee employee) {
        LocalDate beanDate= employee.getBeginContract();
       LocalDate endDate= employee.getEndContract();
        long days = beanDate.until(endDate, ChronoUnit.DAYS);
        DecimalFormat decimalFormat=new DecimalFormat("##.00");
        employee.setContractTerm(Double.parseDouble(decimalFormat.format(days/365.0)));
        if(employeeMapper.insert(employee)==1)
        {
          /*  //发送消息
            Employee emp=employeeMapper.getEmployee(employee.getId()).get(0);
            rabbitTemplate.convertAndSend("mailwoke",emp);*/
            return RespBean.success("添加成功");
        }
        return RespBean.success("添加失败");
    }

    @Override
    public List<Employee> getEmployee(Integer id) {
        return employeeMapper.getEmployee(id);
    }

    @Override
    public RespPageBean getEmloyeeWithSalary(Integer page, Integer pageSize) {
        //开启分页
        Page pageInfo=new Page(page,pageSize);
       IPage<Employee>  employeeIPage=employeeMapper.getEmloyeeWithSalary(pageInfo);
       RespPageBean pageBean=new RespPageBean(employeeIPage.getTotal(),employeeIPage.getRecords());
        return pageBean;
    }
}
